﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmStandardServiceItemPlan : FormBase
    {
        private DBEntityServiceItem _serviceItem;
        private DataSet dsServiceItemDetail;

        public frmStandardServiceItemPlan()
        {
            InitializeComponent();
            ButtonItemIconHelper.InitRibbonBar(this.rbDetails);
        }

        public frmStandardServiceItemPlan(DBEntityServiceItem item)
        {
            InitializeComponent();
            this._serviceItem = item;
            ButtonItemIconHelper.InitRibbonBar(this.rbDetails);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmStandardServiceItemPlan_Shown(object sender, EventArgs e)
        {
            lblServiceItemName.Text = _serviceItem.SNAME;
            lblServiceItemCode.Text = _serviceItem.SERVICEITEMCODE;
            lblServiceTime.Text = _serviceItem.STIMESPAN.ToString();
            lblPrice.Text = _serviceItem.SPRICE.ToString();
            lblStartTime.Text = _serviceItem.STARTDATE.ToString("yyyy-MM-dd");
            lblEndTime.Text = _serviceItem.SENDDATE.ToString("yyyy-MM-dd");

            InitServiceItemDetail();
        }

        private void InitServiceItemDetail()
        {
            string error = string.Empty;
            bool bFlag = BussinessProxyManager.Instance.QueryServiceItemDetail(_serviceItem.SERVICEITEMID, out dsServiceItemDetail, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取服务项目最佳护理周期出错：" + error);
                return;
            }
            gdServiceItemDetail.DataSource = dsServiceItemDetail.Tables[0].DefaultView;
        }

        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Mode = ControlClassModeEnum.emInsert;
            if (form.ShowDialog() == DialogResult.OK)
            {
                DBEntityServiceItemDetail detail = form.ServiceItemDetailInfo;
                DataRow drNew = dsServiceItemDetail.Tables[0].NewRow();
                drNew[DBEntityServiceItemDetail.FieldName.SERVICEITEMID] = _serviceItem.SERVICEITEMID;
                drNew[DBEntityServiceItemDetail.FieldName.SEQNUM] = detail.SEQNUM;
                drNew[DBEntityServiceItemDetail.FieldName.SDAYS] = detail.SDAYS;
                drNew[DBEntityServiceItemDetail.FieldName.SMEMO] = detail.SMEMO;
                dsServiceItemDetail.Tables[0].Rows.Add(drNew);
            }
        }

        private void btnModifyDetail_Click(object sender, EventArgs e)
        {
            int iRowHandler = gvServiceItemDetail.FocusedRowHandle;
            if (iRowHandler < 0)
            {
                MsgBox.MsgInfo("请选择要修改的记录。");
                return;
            }

            DataRow dr = gvServiceItemDetail.GetDataRow(iRowHandler);
            DBEntityServiceItemDetail detail = new DBEntityServiceItemDetail();
            detail.SEQNUM = Convert.ToInt32(dr[DBEntityServiceItemDetail.FieldName.SEQNUM]);
            detail.SDAYS = Convert.ToInt32(dr[DBEntityServiceItemDetail.FieldName.SDAYS]);
            object objMemo = dr[DBEntityServiceItemDetail.FieldName.SMEMO];
            if (objMemo != null && objMemo != DBNull.Value)
            {
                detail.SMEMO = objMemo.ToString();
            }
            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Mode = ControlClassModeEnum.emEdit;
            form.ServiceItemDetailInfo = detail;
            if (form.ShowDialog() == DialogResult.OK)
            {
                detail = form.ServiceItemDetailInfo;
                dr[DBEntityServiceItemDetail.FieldName.SDAYS] = detail.SDAYS;
                dr[DBEntityServiceItemDetail.FieldName.SEQNUM] = detail.SEQNUM;
                dr[DBEntityServiceItemDetail.FieldName.SMEMO] = detail.SMEMO;
            }
        }

        private void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            int iRowHandler = gvServiceItemDetail.FocusedRowHandle;
            if (iRowHandler < 0)
            {
                MsgBox.MsgInfo("请选择要删除的记录。");
                return;
            }
            string sMsg = "确定要删除此记录吗？";
            if (MsgBox.MsgQue(sMsg))
            {
                DataRow dr = gvServiceItemDetail.GetDataRow(iRowHandler);
                dr.Delete();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gvServiceItemDetail.RowCount <= 0)
            {
                MsgBox.MsgInfo("请先设置周期。");
                return;
            }
            if (!dsServiceItemDetail.HasChanges())
            {
                return;
            }
            string error = string.Empty;
            bool bFlag = BussinessProxyManager.Instance.SubmitServiceItmeDetail(dsServiceItemDetail, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("保存服务项目最佳方案失败，" + error);
                return;
            }
            else
            {
                MsgBox.MsgInfo("保存成功。");
                InitServiceItemDetail();
            }
        }

        private void gvServiceItemDetail_DoubleClick(object sender, EventArgs e)
        {
            this.btnModifyDetail_Click(null, null);
        }
    }
}
